import { nextTick, ref, computed } from 'vue'
import { Zondy_docLayer } from './initMap'
import Point from '@/assets/images/Point.png'
import Area from '@/assets/images/Area.png'
import LineString from '@/assets/images/LineString.png'
export const useCatalog = () => {
  // 目录数据的属性
  const defaultProps = {
    children: 'children',
    label: 'label',
    disabled: 'disabled'
  }
  // 目录树
  const treeRef = ref()
  //目录数据
  const catalogInfo = ref([
    {
      id: '111',
      label: '地图文档',
      disabled: true,
      children: [
        {
          id: '222',
          label: '新地图1',
          children: [],
          disabled: true
        }
      ]
    }
  ])
  // 添加图标
  const geoIcon = computed(() => {
    return (type) => {
      switch (type) {
        case 'Reg':
          return Area
        case 'Lin':
          return LineString
        case 'Pnt':
          return Point
      }
    }
  })
  /* ------------------ 默认选中的目录 --------------------- */
  nextTick(() => {
    setTimeout(() => {
      treeRef.value.setCheckedKeys([0, 1, 2, 3, 4, 5], true)
    }, 300)
  })
  /* ------------------ 获取图层的目录信息 --------------------- */
  // 获取guanggu图层的目录信息
  const docCatalog = new Zondy.Catalog.MapDoc({
    docName: 'guanggu'
  })
  var children = []
  docCatalog.getLayersInfo((data) => {
    data.value.forEach((item) => {
      let { LayerIndex, Name } = item
      children.push({
        id: LayerIndex,
        label: Name,
        geoType: item.GeomType
      })
    })
    catalogInfo.value[0].children[0].children = children
  })
  /* ------------------ 设置要素的显示与隐藏 方式1 --------------------- */
  // Zondy_docLayer.setLayerStatus('1,2,3', 'show')
  // show 只显示show参数指定了图层序号的图层
  // const handleChange = (data, checked) => {
  //   // console.log(checked.checkedKeys.join())
  //   Zondy_docLayer.setLayerStatus(checked.checkedKeys.join(), 'show')
  // }
  /* ------------------ 设置要素的显示与隐藏 方式2 --------------------- */
  // include 显示
  // exclude 隐藏
  let flag = ref(true)
  const handleChange = (data, checked) => {
    flag.value = !flag.value
    let state = flag.value ? 'include' : 'exclude'
    Zondy_docLayer.setLayerStatus(data.id, state)
  }
  return { defaultProps, catalogInfo, treeRef, handleChange, geoIcon }
}
